﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextEditUseCase.cs" company="">
//   
// </copyright>
// <summary>
//   The text edit use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System;

    using Libium.Core;

    /// <summary>
    ///   The text edit use case.
    /// </summary>
    public abstract class TextEditUseCase : TextUseCase, ITextEditUseCase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TextEditUseCase"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <param name="service">
        /// The service. 
        /// </param>
        protected TextEditUseCase(ICompoundUseCase parent, IUseCaseInstanceService service)
            : base(parent, service)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets or sets a value indicating whether empty.
        /// </summary>
        public override bool Empty
        {
            get
            {
                return this.Text == string.Empty;
            }

            protected set
            {
                base.Empty = value;
            }
        }

        /// <summary>
        ///   Gets or sets the format.
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public string Format
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        ///   Gets or sets the max char count.
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public int MaxCharCount
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        ///   Gets or sets the min char count.
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public int MinCharCount
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        ///   Gets or sets the text.
        /// </summary>
        public override string Text
        {
            get
            {
                return base.Text;
            }

            set
            {
                if (base.Text == value)
                {
                    return;
                }

                this.SetValue(value);
                this.OnTextChanged();
                this.Modified = true;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The make empty.
        /// </summary>
        public override void MakeEmpty()
        {
            base.MakeEmpty();
            this.Text = string.Empty;
        }

        #endregion

        #region Methods

        /// <summary>
        ///   The build copy options.
        /// </summary>
        /// <returns> The <see cref="CopyOptions" /> . </returns>
        protected override CopyOptions BuildCopyOptions()
        {
            var options = base.BuildCopyOptions();
            options.CanCopy = true;
            options.CanMove = true;
            return options;
        }

        /// <summary>
        /// The build paste options.
        /// </summary>
        /// <param name="data">
        /// The data. 
        /// </param>
        /// <returns>
        /// The <see cref="PasteOptions"/> . 
        /// </returns>
        protected override PasteOptions BuildPasteOptions(ICopyData data)
        {
            var options = base.BuildPasteOptions(data);
            var can = data.IsTextExist();
            options.CanCopy |= can;
            options.CanMove |= can;
            return options;
        }

        /// <summary>
        /// The do finish copy.
        /// </summary>
        /// <param name="action">
        /// The action. 
        /// </param>
        protected override void DoFinishCopy(CopyAction action)
        {
            base.DoFinishCopy(action);
            if (action == CopyAction.Move)
            {
                this.Text = string.Empty;
            }
        }

        /// <summary>
        /// The do paste.
        /// </summary>
        /// <param name="data">
        /// The data. 
        /// </param>
        /// <param name="action">
        /// The action. 
        /// </param>
        protected override void DoPaste(ICopyData data, CopyAction action)
        {
            var text = data.GetText(NotExistsAction.ReturnNull);
            if (text != null)
            {
                this.Text = text;
                return;
            }

            base.DoPaste(data, action);
        }

        /// <summary>
        ///   The do start copy.
        /// </summary>
        /// <returns> The <see cref="ICopyData" /> . </returns>
        protected override ICopyData DoStartCopy()
        {
            var data = base.DoStartCopy();
            data.SetText(this.Text);
            return data;
        }

        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="value">
        /// The value. 
        /// </param>
        protected abstract void SetValue(string value);

        #endregion
    }
}